package bancosys.tec.persist.span.fat;

import java.io.Serializable;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Restrictions;

import bancosys.tec.persist.dao.BaseDAO;
import bancosys.tec.persist.dao.BeanNotFoundException;

/**
 * @created Mar 2, 2007
 * @author Rafael Volpato
 * @param <T> classe do Bean.
 */
public abstract class FatSpanAwareDAO<T extends FatSpanAwareBean<?>> extends BaseDAO<T> {

    /**
     * Procura Bean pela pk da vigência.
     * 
     * @param pk primary key da vigencia.
     * @return o bean encontrado.
     * @throws BeanNotFoundException se nenhum bean puder ser encontrado.
     */
    public T findByVigenciaPk(Serializable pk) throws BeanNotFoundException {
        Criteria crit = this.getSession().createCriteria(this.getPersistableClass());
        crit.createCriteria("vigencias").add(Restrictions.eq("id", pk));
        crit.setFetchMode("vigencias", FetchMode.JOIN);
        return this.executeSingleQuery(crit);

    }
}